/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.render;

import org.retabel.persistence.entity.document.ContentTypeEnum;
import org.retabel.persistence.entity.document.DocumentConfiguration;
import org.retabel.persistence.entity.document.ExportConfiguration;
import org.retabel.persistence.entity.document.SectionConfiguration;
import org.retabel.persistence.entity.report.RenderingEngineEnum;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Abstract base class intended for splitting a given document configuration into rendering engine specific atomic export jobs.
 * Consecutive section configurations with the same rendering engine are grouped together into a single export job.
 * An instance of the generic parameter class is associated with each export job. A new instance of the associated generic parameter
 * class is created by {@link #createNew(SectionConfiguration)}.
 *
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 22:03 17.10.11
 *
 * @author Peter Herber
 * @version 1.0
 */
abstract class DocumentConfigurationSplitter<T> {

    /**
     * Create a new instance of the generic parameter class associated with each section configuration
     *
     * @param sectionConfiguration the section configuration the parameter class is associated to
     * @return a new instance of the generic parameter class
     * @throws Exception thrown if an exception occurs
     */
    public abstract T createNew(SectionConfiguration sectionConfiguration) throws Exception;

    /**
     * Splits the document configuration into atomic rendering engine specific export jobs for each export configuration content type
     *
     * @param documentConfiguration the document configuration to split
     * @return the map of separated rendering engine specific export jobs for each export configuration content type
     * @throws Exception thrown if an exception occurs
     */
    public Map<ContentTypeEnum, List<RenderingEngineJob<T>>> split(DocumentConfiguration documentConfiguration) throws Exception {

        // group equal consecutive rendering engines and group them by content type
        Map<ContentTypeEnum, List<RenderingEngineJob<T>>> renderingEngineGroupMap = new HashMap<>();

        for (SectionConfiguration sectionConfiguration : documentConfiguration.getSectionConfigurations()) {
            RenderingEngineEnum renderingEngine =
                sectionConfiguration.getReportDesign().getReportDesignPackage().getRenderingEngine();

            for (ExportConfiguration exportConfiguration : sectionConfiguration.getExportConfigurations()) {
                ContentTypeEnum contentType = exportConfiguration.getContentType();

                List<RenderingEngineJob<T>> renderingEngineGroups = renderingEngineGroupMap.get(contentType);

                if (renderingEngineGroups == null) {
                    renderingEngineGroups = new LinkedList<>();
                    renderingEngineGroupMap.put(contentType, renderingEngineGroups);

                    RenderingEngineJob<T> renderingEngineGroup = new RenderingEngineJob<>(renderingEngine);

                    renderingEngineGroups.add(renderingEngineGroup);
                }

                RenderingEngineJob<T> renderingEngineGroup = renderingEngineGroups.get(renderingEngineGroups.size() - 1);
                RenderingEngineEnum lastRenderingeEngine = renderingEngineGroup.getRenderingEngine();

                T parameter = createNew(sectionConfiguration);

                if (lastRenderingeEngine == null || renderingEngine != lastRenderingeEngine) {
                    renderingEngineGroup = new RenderingEngineJob<>(renderingEngine);
                    renderingEngineGroups.add(renderingEngineGroup);
                }
                renderingEngineGroup.getEntries().add(parameter);
            }
        }

        return renderingEngineGroupMap;
    }

}
